from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.dofile import myLogger
from common.dolog import DoLog

class Basepage():
    dl = (By.XPATH, '//a[@href="user.php"]')
    def __init__(self,d=None):
        if d is None:
            self.d = webdriver.WebDriver()
            self.d.get('http://192.168.177.128/index.php')
        else:
            self.d=d
        self.d.maximize_window()
        self.loger=DoLog().getLogger(__name__)
        self.d.implicitly_wait(5)
    def findELe(self,locator):
        try:
            print('开始查找元素')
            ele=WebDriverWait(self.d,10,1).\
                until(lambda d:d.find_element(*locator))
        except Exception as e:
            print(f"查找元素异常{locator},异常信息{e}")
        else:
            print(f"元素找到了{locator}")
            return ele
        finally:
            print(f"查找完成{locator}")
    def inputText(self,ele,msg):
        try:
            print('开始输入元素')
            ele.send_keys(msg)
        except Exception as e:
            print(f"输入出现异常{ele}，异常信息为{e}")
        else:
            print(f"元素输入完成{ele}")
        finally:
            print(f"输入完成{ele}")
    def dlchlick(self,ele):
        try:
            print('开始点击')
            ele.click()
        except Exception as e:
            print(f"点击出现异常{ele},异常信息为{e}")
        else:
            print(f"点击完成{ele}")
        finally:
            print(f"点击完成{ele}")

    def getAlrettxt(self):
        try:
            print('获取弹窗文本')
            alert: Alert = self.d.switch_to.alert
            t = alert.text
            alert.accept()
        except Exception as e:
            print(f"获取弹窗，异常信息为{e}")
        else:
            print(f"")
            return t
        finally:
            print(f"获取文本完成")
           # 获取元素的文本

    def getEleTxt(self, ele):
        try:
            print("获取元素的文本")
            myLogger.info("我是info级别的信息")
            eleTxt = ele.text
        except Exception as e:
            print(f"获取元素的文本发生异常{ele},异常信息{e}")
            myLogger.info("我是info级别的信息")
        else:
            print(f"获取元素的文本成功{ele}")
            myLogger.info("我是info级别的信息")
            return eleTxt
        finally:
            print(f"获取元素的文本完成{ele}")
            myLogger.info("我是info级别的信息")
          # 获取弹窗的文本

    def getAlertTxt(self):
        try:
            print('获取弹窗的文本~~~~~')
            alert: Alert = self.driver.switch_to.alert
            t = alert.text
            alert.accept()
        except Exception as e:
            print(f'获取弹窗的文本发生异常,异常信息{e}')
            myLogger.info('我是info级别的信息')
        else:
            print(f'获取弹窗的文本成功~~~~~~~~~')
            myLogger.info('我是info级别的信息')
            return t
        finally:
            print(f'获取弹窗的文本完成~~~~~')
            myLogger.info('我是info级别的信息')
